package com.yair_srm.integration.sap.impl;

import java.util.ArrayList;
import java.util.HashMap;

import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;
import com.yair_srm.integration.IPOItemSAO;
import com.yair_srm.integration.IPOSAO;
import com.yair_srm.integration.sap.IPOItem_SAP;
import com.yair_srm.integration.sap.ISAP;
import com.yair_srm.model.bean.History;
import com.yair_srm.model.bean.PurchaseOrder;
import com.yair_srm.model.bean.PurchaseOrderItem;
import com.yair_srm.model.bean.SechduleLine;
import com.yair_srm.model.bean.VConfirmation;

public class POItem_SAP implements IPOItem_SAP, IPOItemSAO {
	private SAP_AS_POOL sapconnector = SAP_AS_POOL.getInstance();
	private JCoFunction function;

	public POItem_SAP() {
		sapconnector = SAP_AS_POOL.getInstance();
	}

	public PurchaseOrderItem getPOItemDetail(String vendor, String ponumber,
			String itemno) {
		// TODO Auto-generated method stub
		try {
			function = this.fetchfunction("ZSRM_GET_POITEMDETAIL");
		} catch (JCoException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		PurchaseOrderItem item = new PurchaseOrderItem();

		JCoParameterList import_p = function.getImportParameterList();
		import_p.setValue("PONO", ponumber);
		import_p.setValue("ITEMNO", itemno);
		import_p.setValue("VENDOR", vendor);

		try {
			function.execute(SAP_AS_POOL.getDestination());

			JCoTable sechdulelines = function.getTableParameterList().getTable(
					"SECHDULELINE");
			JCoTable confirmation1 = function.getTableParameterList().getTable(
					"CONFIRMATION1");
			JCoTable confirmation2 = function.getTableParameterList().getTable(
					"CONFIRMATION2");
			JCoTable history = function.getTableParameterList().getTable(
					"HISTORY");
			
			if (!sechdulelines.isEmpty())
				do {
					SechduleLine sechduleline = new SechduleLine();
					sechduleline.setCounter(sechdulelines.getString("ETENR"));
					sechduleline.setDel_date(sechdulelines.getString("EINDT"));
					sechduleline.setQuantity(sechdulelines.getString("MENGE"));
					System.out.println("add sechduleline");
					System.out.println(sechduleline.getCounter());
					System.out.println(sechduleline.getDel_date());
					System.out.println(sechduleline.getOrder_id());
					System.out.println(sechduleline.getQuantity());
					item.addSechduleline(sechduleline);
				} while (sechdulelines.nextRow());
			
			if (!confirmation1.isEmpty())
				do {
					VConfirmation confirmation = new VConfirmation();
					
					confirmation.setCounter(confirmation1.getString("ETENS"));
					confirmation.setDel_date(confirmation1.getString("EINDT"));
					confirmation.setQuantity(confirmation1.getString("MENGE"));
					confirmation.setCreation_date(confirmation1.getString("ERDAT"));
					
					item.addConfirmation1(confirmation);
				} while (confirmation1.nextRow());
			
			if (!confirmation2.isEmpty())
				do {
					VConfirmation confirmation = new VConfirmation();
					confirmation.setCounter(confirmation2.getString("ETENS"));
					confirmation.setDel_date(confirmation2.getString("EINDT"));
					confirmation.setQuantity(confirmation2.getString("MENGE"));
					confirmation.setCreation_date(confirmation2.getString("ERDAT"));
					item.addConfirmation2(confirmation);
				} while (confirmation2.nextRow());

			
			if(!history.isEmpty())
				do{
					History h = new History();
					h.setAmount(history.getString("AMOUNT"));
					h.setDate(history.getString("DATE"));
					h.setQuantity(history.getString("QUANTITY"));
					h.setType(history.getString("TYPE"));
					item.addHistory(h);
					System.out.println("add history");
				}while(history.nextRow());
		} catch (JCoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return item;
	}

	public JCoFunction fetchfunction(String functionname) throws JCoException {

		function = null;

		try {
			function = sapconnector.fetchfunction(functionname);
		} catch (JCoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return function;
	}

	public static void main(String[] args) {
		POItem_SAP e = new POItem_SAP();
		PurchaseOrderItem pi = e.getPOItemDetail("3411", "3000000095", "1");
		ArrayList<SechduleLine> s = pi.getSechduleLines();
		for (int i = 0; i < s.size(); i++) {
			SechduleLine l = s.get(i);
			System.out.println(l.getCounter() + "/" + l.getDel_date() + "/"
					+ l.getQuantity());
		}
	}

}
